package com.lianbang.pharmacy.module.main

import android.app.Activity
import android.content.Intent
import com.lianbang.pharmacy.R
import cn.ubest.mvvmcore.base.BaseFragment
import com.lianbang.pharmacy.databinding.FragmentUserBinding
import android.os.Bundle
import android.view.View
import android.widget.Switch
import androidx.lifecycle.Observer
import cn.ubest.mvvmcore.utils.DoubleClickHelper
import cn.ubest.mvvmcore.utils.GlideUtils
import cn.ubest.mvvmcore.utils.LogUtils
import cn.ubest.mvvmcore.utils.ResourcesUtils
import com.bumptech.glide.load.model.GlideUrl
import com.gyf.barlibrary.ImmersionBar
import com.jeremyliao.liveeventbus.LiveEventBus
import com.lianbang.pharmacy.module.user.*
import com.lianbang.pharmacy.util.LoginUtils
import com.lianbang.pharmacy.vm.MainVM
import com.lianbang.pharmacy.vm.UserVM
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.android.synthetic.main.fragment_user.*
import kotlinx.android.synthetic.main.fragment_user.view_status_bar

class UserFragment : BaseFragment<UserVM, FragmentUserBinding>(), View.OnClickListener {

    companion object {
        fun newInstance(): UserFragment {
            val fragment = UserFragment()
            return fragment
        }
    }

    override fun initLayout(): Int {
        return R.layout.fragment_user
    }

    override fun initData(arguments: Bundle?) {
        LiveEventBus.get("login", Boolean::class.java).observe(this, Observer {
            if (it) {
                updateUser()
            }
        })
    }

    override fun initView(view: View) {
        activity?.also {
            val statusBarHeight = ImmersionBar.getStatusBarHeight(it)
            view_status_bar.layoutParams.height = statusBarHeight
        }
        updateUser()
        btn_score.setOnClickListener(this)
        btn_coupon.setOnClickListener(this)
        btn_order.setOnClickListener(this)
        btn_order_all.setOnClickListener(this)
        btn_order_processing.setOnClickListener(this)
        btn_order_finished.setOnClickListener(this)
        btn_message.setOnClickListener(this)
        btn_address.setOnClickListener(this)
        btn_qualification.setOnClickListener(this)
        btn_setting.setOnClickListener(this)
        btn_sign.setOnClickListener(this)

    }

    override fun loadData() {

    }

    private fun updateUser() {
        LoginUtils.mUserBean?.also {
            GlideUtils.display(activity, it.avatar, civ_header)
            tv_name.text = it.nickname
            tv_desc.text = it.mark
            tv_sign.text = if (it.is_day_sgin) "已签到" else "未签到"
            tv_score.text = it.integral
            tv_coupon.text = "${it.couponCount}"
            tv_message_count.text = "${it.notice}"
        }
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.btn_score -> MyScoreActivity.start(activity)
            R.id.btn_coupon -> MyCouponListActivity.start(activity)
            R.id.btn_order -> OrderActivity.start(activity, 0)
            R.id.btn_order_all -> OrderActivity.start(activity, 0)
            R.id.btn_order_processing -> OrderActivity.start(activity, 1)
            R.id.btn_order_finished -> OrderActivity.start(activity, 3)
            R.id.btn_message -> ""
            R.id.btn_address -> MyAddressActivity.start(activity)
            R.id.btn_qualification -> ""
            R.id.btn_setting -> ""
            R.id.btn_sign -> {
                LoginUtils.mUserBean?.also {
                    if (!it.is_day_sgin) {
                        SignActivity.start(this)
                    }
                }
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == SignActivity.REQ_CODE) {
                mViewModel.getUser()
            }
        }
    }


}